package org.rjxh.examfront.handler;

import com.alibaba.fastjson.JSON;
import org.rjxh.examfront.pojo.R;
import org.rjxh.examfront.web.tools.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        R responseResult =
                new R<>(HttpStatus.UNAUTHORIZED.value(), authException.getMessage());
        WebUtils.renderString(response, JSON.toJSONString(responseResult));
    }
}
